package com.example.trackcircuitdiagnosis.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.trackcircuitdiagnosis.entity.DeviceInformationEntity;
import com.example.trackcircuitdiagnosis.entity.MonitoringProjectEntity;
import com.example.trackcircuitdiagnosis.service.DeviceInformationService;
import com.example.trackcircuitdiagnosis.service.MonitoringProjectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@CrossOrigin
@RequestMapping("/deviceinformation")
public class DeviceInformationController {

    @Autowired
    private DeviceInformationService deviceInformationService;

    // 新增或者更新
    @PostMapping
    @Operation(description = "更新设备模拟量信息")
    public boolean save(@RequestBody DeviceInformationEntity deviceInformationEntity) {
        return deviceInformationService.saveOrUpdate(deviceInformationEntity);
    }

    @DeleteMapping("/{id}")
    @Operation(
            description = "删除监控设备",
            parameters = {
                    @Parameter(name = "mid", description = "要删除的设备模拟量信息id")
            }

    )
    public Boolean delete(@PathVariable Integer id) {
        return deviceInformationService.removeById(id);
    }

    @PostMapping("/del/batch")
    @Operation(
            description = "批量删除设备模拟量",
            parameters = {
                    @Parameter(name = "ids", description = "要批量删除的设备模拟量")
            }

    )
    public boolean deleteBatch(@RequestBody List<Integer> ids) {
        return deviceInformationService.removeByIds(ids);
    }

    @GetMapping
    @Operation(description = "查找全部")
    public List<DeviceInformationEntity> findAll() {
        return deviceInformationService.list();
    }

    @GetMapping("/{id}")
    @Operation(
            description = "查找监控设备模拟量",
            parameters = {
                    @Parameter(name = "id", description = "要查找设备模拟量id")
            }

    )
    public DeviceInformationEntity findOne(@PathVariable Integer id) {
        return deviceInformationService.getById(id);
    }

    @GetMapping("/page")
    @Operation(
            description = "根据did查询对应的deviceinformation区段监控的模拟量信息",
            parameters = {
                    @Parameter(name = "did", description = "did"),
                    @Parameter(name = "pageNum", description = "要查找信息页数"),
                    @Parameter(name = "pageSize", description = "要查找信息每一页的数量")
            }
    )
    public Page<DeviceInformationEntity> findPageByDid(@RequestParam Integer did,
                                                       @RequestParam Integer pageNum,
                                                       @RequestParam Integer pageSize) {
        QueryWrapper<DeviceInformationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("did",did);
        queryWrapper.orderByDesc("id");
        return deviceInformationService.page(new Page<>(pageNum, pageSize), queryWrapper);
    }
}
